package com.magi.core;

import java.util.Iterator;
import java.util.List;

public class TableMetaData {
    private final String tableName;
    private final List<Column> columns;

    public TableMetaData(String tableName, List<Column> columns) {
        this.tableName = tableName;
        this.columns = columns;
    }

    private Column getColumn(String name) {
        for (Column f : this.columns) {
            if (f.getName().equals(name)) { return f; }
        }
        return null;
    }

    public Column[] getColumns() {
        Column[] ret = new Column[this.columns.size()];
        return this.columns.toArray(ret);
    }

    public Iterator<Column> getColumnIterator() {
        return new Iterator<Column>() {
            private final List<Column> columns = TableMetaData.this.columns;
            private int index = 0;
            @Override
            public boolean hasNext() {
                return index < columns.size();
            }

            @Override
            public Column next() {
                index += 1;
                return columns.get(index - 1);
            }
        };
    }

    public String getTableName() {
        return this.tableName;
    }

    public static class Column {
        private final String type;
        private final String name;
        private boolean isPrimaryKey = false;


        public Column(String type, String name) {
            this.type = type;
            this.name = name;
        }

        public boolean isPrimaryKey() {
            return isPrimaryKey;
        }

        public void setPrimaryKey(boolean primaryKey) {
            isPrimaryKey = primaryKey;
        }

        public String getType() {
            return type;
        }

        public String getName() {
            return name;
        }
    }
}
